package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.User_Limit;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class GeRen_Update_Msg_GXQM_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back;
    private EditText et_feedback;
    private TextView tv_feedback_tijiao, tv_feedback_count;
    private User_Limit user_limit;
    private String access_token;
    private SharedPreferences preferences;
    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geren_main_update_msg_gxqm);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        user_limit = (User_Limit) intent.getSerializableExtra("user_limit");
        back = findViewById(R.id.back);
        et_feedback = findViewById(R.id.et_feedback);
        tv_feedback_tijiao = findViewById(R.id.tv_feedback_tijiao);
        tv_feedback_count = findViewById(R.id.tv_feedback_count);
        tv_feedback_tijiao.setOnClickListener(this);
        back.setOnClickListener(this);
        preferences = GeRen_Update_Msg_GXQM_Activity.this.getSharedPreferences("update_message", Context.MODE_PRIVATE);
        et_feedback.setText("" + user_limit.getRemark());
        et_feedback.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tv_feedback_count.setText(count + "/128");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_feedback_tijiao:
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("gxqm", et_feedback.getText().toString().trim());
                editor.apply();
                JSONObject object = new JSONObject();
                try {
                    object.put("remark", et_feedback.getText().toString().trim());//签名
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                postData(object);
                finish();
                break;
            case R.id.back:
                SharedPreferences.Editor editor1 = preferences.edit();
                editor1.putString("gxqm", "");
                editor1.apply();
                JSONObject object1 = new JSONObject();
                try {
                    object1.put("remark", et_feedback.getText().toString().trim());//签名
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                postData(object1);
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     * @param object
     */
    private void postData(JSONObject object) {
        Log.d("xinyun", object.toString());
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.fill_information_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("xinyun", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONObject info = jsonObject.getJSONObject("info");
                        ToolUtils.showToast(GeRen_Update_Msg_GXQM_Activity.this, message);
                        finish();
                    } else {
                        ToolUtils.showToast(GeRen_Update_Msg_GXQM_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(GeRen_Update_Msg_GXQM_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
